package com.tao.power.Service.ServiceImpl;

import cn.hutool.core.convert.Convert;
import com.tao.power.Dao.RolerMapper;
import com.tao.power.Dao.UserMapper;
import com.tao.power.Dto.Result;
import com.tao.power.Enums.ErrorEnums;
import com.tao.power.Pojo.*;
import com.tao.power.Service.RolerService;
import com.tao.power.Utils.SystemExceptionNew;
import com.tao.power.Utils.verifyUtils;
import com.tao.power.config.AuthorityAnnotation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RolerServiceImpl implements RolerService {

    @Resource
    private RolerMapper rolerMapper;
    @Resource
    private verifyUtils verifyUtils;

    @Override
    public Result queryRoler(int count, int page) {
        int index = count * (page - 1);
        List<Roler> rolerList = rolerMapper.queryRoler(count, index);
        return Result.ok("", rolerList, (long) rolerMapper.rolerTotal(), ErrorEnums.Success.getValue());
    }

    @Override
    @Transactional
    public Result deleteRoler(Integer id) {
        if (id == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        List<UserRoler> list =rolerMapper.queryByRolerId(id);
        if (!list.isEmpty()){
            return Result.fail(ErrorEnums.RolesFail.getLabel(),ErrorEnums.RolesFail.getValue());
        }
        int count = rolerMapper.deleteRoler(id);
        rolerMapper.deleteRolesPower(id);
        rolerMapper.deleteUserRoles(id);
        if (count == 0) {
            throw new SystemExceptionNew(ErrorEnums.Deleteinfo_Fail.getValue(), ErrorEnums.Deleteinfo_Fail.getLabel());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result updateRoler(Roler roler) {
        if (roler == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        verifyUtils.rolesVerify(roler);
        int count = rolerMapper.updateRoler(roler);
        if (count == 0) {
            return Result.fail(ErrorEnums.Info_Fail.getLabel(), ErrorEnums.Info_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result insertRoler(Roler roler) {
        if (roler == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        verifyUtils.rolesVerify(roler);
        int count = rolerMapper.insertRoler(roler);
        if (count == 0) {
            return Result.fail(ErrorEnums.AddInFO_Fail.getLabel(), ErrorEnums.AddInFO_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result queryRolerBySearch(String rolerName, String rolerStatus, Date startTime, Date endTime
            , Integer currentPage, Integer pageSize) {
        int count = pageSize * (currentPage - 1);
        List<Roler> rolerList = rolerMapper.queryRolerBySearch(rolerName, rolerStatus, startTime, endTime
                , count, pageSize);
        return Result.ok("", rolerList, (long) rolerMapper.queryRolerBySearchTotal(rolerName, rolerStatus, startTime, endTime),
                ErrorEnums.Success.getValue());
    }

    @Override
    @Transactional
    public Result batchDel(List<Integer> idList) {
        if (idList.isEmpty()) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        int count = rolerMapper.BatchDel(idList);
        int batchDeleteRolesPower = rolerMapper.batchDeleteRolesPower(idList);
        if (count == 0 && batchDeleteRolesPower == 0) {
            throw new SystemExceptionNew(ErrorEnums.Deleteinfo_Fail.getValue(),ErrorEnums.Deleteinfo_Fail.getLabel());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result queryRoles() {
        List<Menu> menus = new ArrayList<>();
        List<Menu> rolesList = rolerMapper.queryRoles();
        Map<Integer, Menu> rolesMap = rolesList.stream().collect(Collectors.toMap(Menu::getId, menu -> menu));
        for (Menu menu : rolesList) {
            if (menu.getMenuIdentfication() != null) {
                Menu parent = rolesMap.get(Integer.parseInt(menu.getMenuIdentfication()));
                parent.getChildren().add(menu);
            } else {
                menus.add(menu);
            }
        }
        return Result.ok("", menus, ErrorEnums.Success.getValue());
    }

    @Override
    public Result queryRolesById(Integer id) {
        if (id == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        List<Integer> list = new ArrayList<>();
        RolesPower rolesPower = rolerMapper.queryRolesPowerByRolesId(id);
        if (rolesPower == null) {
            return Result.ok("", list, ErrorEnums.Success.getValue());
        }
        String power = rolesPower.getPowerIds();
        if (power == null || power.equals("")) {
            return Result.ok("", list, ErrorEnums.Success.getValue());
        }
        List<Integer> powerIds = (List<Integer>) Convert.toList(power);

        return Result.ok("", powerIds, ErrorEnums.Success.getValue());
    }

    @Override
    public Result updateRoles(List<Integer> list, Integer id) {
        if (id == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        //获取前端传来的权限集合，转换成string
        String power = Convert.toStr(list);
        //判断数据库是否存在该角色的权限
        RolesPower rolesPower = rolerMapper.queryRolesPowerByRolesId(id);
        if (rolesPower == null) {
            //不存在新增权限集
            int count = rolerMapper.insertRolesPower(power, id);
            if (count == 0) {
                return Result.fail(ErrorEnums.Info_Fail.getLabel(), ErrorEnums.Info_Fail.getValue());
            }
            return Result.ok("", ErrorEnums.Success.getValue());
        }
        //存在更新权限集
        int count = rolerMapper.updateRolesPower(power, id);
        if (count == 0) {
            return Result.fail(ErrorEnums.Info_Fail.getLabel(), ErrorEnums.Info_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result updateStatus(Roler roler) {
        if (roler == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        int count = rolerMapper.updateRoler(roler);
        if (count == 0) {
            return Result.fail(ErrorEnums.Info_Fail.getLabel(), ErrorEnums.Info_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }
}
